package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        for(int i=0;i<list.size();i++){
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏的绑定车辆总数
            int totalNum = vehicleVOList.size();
            //在定义变量保存可用车辆数
            int availableNum = 0;
            for(VehicleVO vehicleVO:vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //给当前围栏对象设置可用车辆数
            if(availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else {
                geofenceVO.setAvailableNum(0);
            }
            //给围栏对象设置车辆总数
            geofenceVO.setTotalNum(totalNum);
            //后续进一步查看车辆详询是会使用车辆数据，所以一起粗如geofenceVO返回给前端
            geofenceVO.setVehicleList(vehicleVOList);
        }

        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        //前端传过来的数据是K-V结构
        //{type:'circle', longitude: 116, latitude: 39, radius: 661}
        //{type:'rectangle', recPoints: '116-39,116-39,116-39,116-39'}
        HashMap<String,String> position = geofenceParam.getPosition();
        //准备JSON处理库工具
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //这个方法如果传入null值或者字段不匹配会抛出异常,所以放置于try-catch中来处理即可
            String positionStr = objectMapper.writeValueAsString(position);
            if(geofenceParam.getId()!=null){//更新围栏
                geofenceMapper.update(geofence);
            }else{//新增围栏
                geofence.setStatus("1");//启用状态
                geofence.setPosition(positionStr);
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if(list!=null&&list.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            geofenceMapper.deleteById(id);
        }
    }
}
